package mutiThread;

/**
 * Created by thinkpad on 2015/7/8.
 */
public class SyncTest {

    public static class Thread1 implements Runnable {
        public void run() {
            synchronized (MqService.getSingleton()) {
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(502);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "Thread1  synchronized loop " + i);
                }
            }
        }
    }

    public static class Thread2 implements Runnable {
        public void run() {
            synchronized (MqService.getSingleton()){
                 try {
                        Thread.sleep(600);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                for (int i = 0; i < 5; i++) {
//                    try {
//                        Thread.sleep(500);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    System.out.println(Thread.currentThread().getName() + "Thread2 synchronized loop " + i);
                    MqService.getSingleton().sendMsg();
                }
            }
        }
    }
    public static void main(String[] args) {
        Thread1 t1 = new Thread1();
        Thread ta = new Thread(t1, "A");
        Thread2 t2 = new Thread2();
        Thread tb = new Thread(t2, "B");
        ta.start();
        tb.start();
    }
}



